<%@ include file="/common/taglibs2.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@ page import="java.util.Map,
                 javax.servlet.jsp.jstl.sql.Result,
                 net.sf.navigator.menu.MenuComponent,
                 net.sf.navigator.menu.MenuRepository"%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Dynamic, Database-driven Menu</title>
    <link rel="stylesheet" type="text/css" media="all" href="<c:url value="/styles/menuExpandable.css"/>" />
    <link rel="stylesheet" type="text/css" media="all" href="<c:url value="/styles/xtree.css"/>" />
    <link rel="stylesheet" type="text/css" media="all" href="<c:url value="/styles/global.css"/>" />
    
    <script type="text/javascript" src="<c:url value="/scripts/menuExpandable.js"/>"></script>    
    <script type="text/javascript" src="<c:url value="/scripts/xtree.js"/>"></script>
    
    <script type="text/javascript">
        /* Function for showing and hiding elements that use 'display:none' to hide */
        function toggleDisplay(targetId) {
            if (document.getElementById) {
                target = document.getElementById(targetId);
                if (target.style.display == "none"){
                    target.style.display = "";
                } else {
                    target.style.display = "none";
                }
            }
        }
    </script>
</head>

<div id="content" style="width: 500px">
<h1>Database Driven Menu</h1>



<sql:setDataSource var="db" url="jdbc:hsqldb:db/menu"
    driver="org.hsqldb.jdbcDriver" user="sa" password=""/>

<sql:transaction dataSource="${db}">

    <sql:update>
        DROP TABLE menu_item IF EXISTS
    </sql:update>
    <sql:update>
        CREATE TABLE menu_item (
           id BIGINT not null,
           parent_name VARCHAR(30),
           name VARCHAR(30),
           title VARCHAR(30),
           description VARCHAR(50),
           location VARCHAR(255),
           target VARCHAR(10),
           onclick VARCHAR(100),
           onmouseover VARCHAR(100),
           onmouseout VARCHAR(100),
           image VARCHAR(50),
           altImage VARCHAR(30),
           tooltip VARCHAR(100),
           roles VARCHAR(100),
           page VARCHAR(255),
           width VARCHAR(5),
           height VARCHAR(5),
           forward VARCHAR(50),
           action VARCHAR(50),
           primary key (id)
        )
    </sql:update>

    <sql:update var="updateCount">
        INSERT INTO menu_item (id, name, title) VALUES (1,'DatabaseMenu','Database Menu')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (2,'DatabaseMenu','Yahoo','Yahoo2 Mail','http://1.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (3,'DatabaseMenu','JavaBlogs','JavaBlogs','http://2.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (5,'DatabaseMenu','JavaBlogs','JavaBlogs5','http://3.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (6,'DatabaseMenu','JavaBlogs','JavaBlogs6','http://4.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (7,'DatabaseMenu','JavaBlogs','JavaBlogs7','http://5.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (8,'DatabaseMenu','JavaBlogs','Office','Link.do?method=office')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (9,'DatabaseMenu','JavaBlogs','Friend','Link.do?method=friends')
    </sql:update>                    
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (13,'DatabaseMenu','JavaBlogs','stkReq','/ittl/StkReq.do?mode=blank')
    </sql:update>    
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, name, title, location)
        VALUES (4,'StandaloneMenu','Standalone Menu','http://raibledesigns.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, name, title) VALUES (10,'DynamicMenu','Dynamic Menu')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (11,'DynamicMenu','JavaBlogs2','Phone','http://phone.com')
    </sql:update>
    <sql:update var="updateCount">
        INSERT INTO menu_item (id, parent_name, name, title, location)
        VALUES (12,'DynamicMenu','JavaBlogs2','Phone2','http://phone2.com')
    </sql:update>            
    <sql:query var="menus">
        SELECT * FROM menu_item order by id;
    </sql:query>

</sql:transaction>
    
    <display:table name="${menus.rows}" class="list" style="width: 600px">
        <display:column property="id"/>
        <display:column property="name"/>
        <display:column property="parent_name" title="Parent Name"/>
        <display:column property="title"/>
        <display:column property="location"/>
    </display:table>              

    <%
        // I had issues using the existing repository - creating a new one
        // seems to solve the problem.  If you figure out how to use the default
        // Repository and keep your menus from duplicating themselves - please
        // let me know!

        MenuRepository repository = new MenuRepository();
        // Get the repository from the application scope - and copy the
        // DisplayerMappings from it.
        MenuRepository defaultRepository = (MenuRepository)
                application.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
        repository.setDisplayers(defaultRepository.getDisplayers());

        Result result = (Result) pageContext.getAttribute("menus");
        Map[] rows = result.getRows();
        for (int i=0; i < rows.length; i++) {
            MenuComponent mc = new MenuComponent();
            Map row = rows[i];
            String name = (String) row.get("name");
            mc.setName(name);
            String parent = (String) row.get("parent_name");
            System.out.println(name + ", parent is: " + parent);
            if (parent != null) {
                MenuComponent parentMenu = repository.getMenu(parent);
                if (parentMenu == null) {
                    System.out.println("parentMenu '" + parent + "' doesn't exist!");
                    // create a temporary parentMenu
                    parentMenu = new MenuComponent();
                    parentMenu.setName(parent);
                    repository.addMenu(parentMenu);
                }

                mc.setParent(parentMenu);
            }
            String title = (String) row.get("title");
            mc.setTitle(title);
            String location = (String) row.get("location");
            mc.setLocation(location);
            repository.addMenu(mc);
        }
        pageContext.setAttribute("repository", repository);
    %>

               
    <pre>&lt;menu:useMenuDisplayer name="ListMenu" repository="repository"&gt;
    &lt;menu:displayMenu name="DatabaseMenu"/&gt;
    &lt;menu:displayMenu name="StandaloneMenu"/&gt;
&lt;/menu:useMenuDisplayer&gt;</pre>

    <p style="padding: 10px 0">Which results in:</p>
    <div class="dynamicMenu">
        <menu:useMenuDisplayer name="ListMenu" repository="repository">
            <menu:displayMenu name="DatabaseMenu"/>
            <menu:displayMenu name="StandaloneMenu"/>
            <menu:displayMenu name="DynamicMenu"/>
            
        </menu:useMenuDisplayer>
    </div>
   
    <pre>&lt;menu:useMenuDisplayer name="Velocity" config="/templates/xtree.html"
    repository="repository"&gt;
    &lt;c:forEach var="menu" items="${repository.topMenus}"&gt;
        &lt;menu-el:displayMenu name="${menu.name}"/&gt;
    &lt;/c:forEach&gt;
&lt;/menu:useMenuDisplayer&gt;</pre>

    <p style="padding: 10px 0">Which results in:</p>

    <div class="dynamicMenu tree">
    <script type="text/javascript">
        <menu:useMenuDisplayer name="Velocity" config="/templates/xtree.html"
            repository="repository" >
          <c:forEach var="menu" items="${repository.topMenus}">
            <menu-el:displayMenu name="${menu.name}" target="tariff"/>
          </c:forEach>
        </menu:useMenuDisplayer>
    </script>
    </div>
</div>


<div id="source">
  <a href="<%=request.getRequestURI()%>.src">View JSP Source</a>
  <br />
  <a href="<%=request.getContextPath()%>/index.jsp">Back to Index</a>
</div>

<a href="/ittl/stockRequest/bcstkissue2form.jsp">issue</a><br />
<a href="Link.do?method=friends">Friends</a><br />
<a href="Link.do?method=office">The Office</a><br />
<%= request.getSession().getAttribute("userName") %>
<a href="<%= request.getContextPath() %>/StkReq.do?mode=blank">Stock Request</a><br />
		<iframe name="tariff" scr="Link.do?method=friends" scrolling="auto" width="95%" height="90%">
		</iframe>

request.getContextPath()=<%= request.getContextPath() %>
</body>
</html>

